

#include "text.h"
#include "draw.h"

using namespace std;


void display()
{
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);//光照启用
	float light0Position[] = { 0.0f,1.0f,10.0f,1.0f };
	glLightfv(GL_LIGHT0, GL_POSITION, light0Position);

	{
		float ambientlight1Color[] = { 0.4f,0.4f,0.4f,1.0f };
		glLightfv(GL_LIGHT0, GL_AMBIENT, ambientlight1Color);
	}

	if (model == 0)
	{
		glDisable(GL_DEPTH_TEST);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}
	if (model == 1)//有消隐
	{
		glEnable(GL_DEPTH_TEST);//开启更新深度缓冲区
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}
	if (model == 2)
	{
		glEnable(GL_DEPTH_TEST);
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}
	glTranslatef(0, 0, -350);
	glRotatef(manlge -2.5, 0, 1, 0);

	glPushMatrix();
	glScalef(10, 10, 10);
	//墙壁
	glPushMatrix(); 
	DrawHouse();
	glPopMatrix(); 


	//桌子
	glPushMatrix();

	//1.世界坐标系平移缩放
	glTranslatef(11.6, -7, -6);//平移
	glScalef(4, 4, 4);//缩放
	DrawDesk();
	glPopMatrix();

	//床
	glPushMatrix();
	glTranslatef(0, 0,-5.2);
	glRotatef(90, 0, 1, 0);
	drawBed();
	glPopMatrix();

	//电脑
	glPushMatrix();
	glTranslatef(11.4, -3.5, -4);
	DrawComputer();
	glPopMatrix();

	//椅子
	glPushMatrix();
	glTranslatef(-5, -9, 0);
	glScalef(4, 4, 4);
	//DrawChair();
	glPopMatrix();

	//表
	glPushMatrix();
	glTranslatef(-9.8, 2, 0);
	glRotatef(90, 0, 1, 0);
	glScalef(2, 2, 1);
	//DrawClock();
	glPopMatrix();

	//灯
	glPushMatrix();
	glTranslatef(0, 4.5, 0);
	
	glRotatef(90, 1, 0, 0);
	DrawLight();
	glPopMatrix();


	//地灯
	glPushMatrix();
	glTranslatef(- 15.5,0 , - 6.4);
	glRotatef(-90, 1, 0, 0);
	DrawLightGan();
	glPopMatrix();
	//抽屉
	glPushMatrix();
	glTranslatef(5.0, -5.5, -8);
	glTranslatef(- 10.5, -3.1, 0.2);
	glScalef(3, 2, 5.5);
	DrawDrawer();
	glPopMatrix();

	//bihua
	glPushMatrix();
	glTranslatef(0.0, 2.6, -10.2);
	glTranslatef(mx, my, mz);
	DrawCube(3, 5, 0.5, g_bitmap[2]);
	glPopMatrix();

	glPopMatrix();
}

//事件
void Glinit() //初始化  
{  
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// //启动深度检测
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
										// Initialization Went OK
}    
void reshape(int width, int height)//窗口大小改变事件  
{  
	if(height == 0)
		height = 1;

	glViewport(0,0,width,height);//视口在屏幕的大小位置 Reset The Current Viewport
    
	glMatrixMode(GL_PROJECTION);// 设置投影方式  Select The Projection Matrix
	glLoadIdentity();           // Reset The Projection Matrix
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,10000.0f);// Calculate The Aspect Ratio Of The Window

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();
	
	  
}  

void keyboard(unsigned char key, int x, int y) //键盘事件 
{  
    switch(key)  
    {  
	case 'S'://点
	case 's'://点
		model = 1;
		break;
	case 'A'://
	case 'a'://
		model = 0;
		break;
	case 'D'://
	case 'd'://
		model = 2;
		break;
	case 'g':
	case 'G':
		manlge += 1;
		break;
	case 'h':
	case 'H':
		manlge -= 1;
		break;

    }  
} 

void DrawGLScene() //重新绘制 
{  

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();// Reset The Current Modelview Matrix
	gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);//相机,三组，相机位置、相机朝向、相机向上的方向
	display();//绘图
	 
	glutSwapBuffers();//交换缓存 
	glutPostRedisplay();

}  
void main(int argc, char **argv)
{
   glutInit(&argc, (char**)argv); //对GLUT进行初始化 
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //模式 双缓冲 ，深度缓存区
   glutInitWindowPosition(100, 100); //指定窗口左上角的
   glutInitWindowSize(600,400); //指定窗口的大小
   glutCreateWindow("OpenGL_Indoor_Scene");  //创建gl窗口
   Glinit();
   LoadTextMap(); //加载图	
   //-------------注册回调函数---------//
   glutDisplayFunc(DrawGLScene);    // 重新绘制事件 
   glutKeyboardFunc(keyboard);  // 键盘事件
  
   glutReshapeFunc(reshape);    //窗口大小发生变化事件

   glutMainLoop();            //调用已注册的回调函数           
}